package com.fjd.service.impl;

import com.fjd.constant.MessageConstant;
import com.fjd.context.BaseContext;
import com.fjd.dto.AddressDTO;
import com.fjd.entity.CustomerAddr;
import com.fjd.exception.AddressBookBusinessException;
import com.fjd.exception.BaseException;
import com.fjd.mapper.CustomerAddrMapper;
import com.fjd.service.AddressService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.Address;
import org.aspectj.bridge.Message;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

@Service
@Slf4j
public class AddressServiceImpl implements AddressService {
    @Autowired
    private CustomerAddrMapper customerAddrMapper;

    /**
     * 新增地址
     * @param addressDTO
     */
    @Transactional
    public void addAddress(AddressDTO addressDTO) {
        CustomerAddr customerAddr = new CustomerAddr();
        BeanUtils.copyProperties(addressDTO,customerAddr);
        customerAddr.setCustomerId(String.valueOf(BaseContext.getCurrentId()));
        customerAddr.setAddressId(UUID.randomUUID().toString().replace("-",""));
        log.info("新增地址：{}",customerAddr);
        //TODO: zipcode邮政编码设置
        customerAddrMapper.insert(customerAddr);
        if(addressDTO.isDefault() || getDefaultByCustomerId() == null) {
            setDefault(customerAddr);
        }
    }

    /**
     * 查询当前用户地址信息
     * @return
     */
    public List<CustomerAddr> list() {
        String customerId = String.valueOf(BaseContext.getCurrentId());
        List<CustomerAddr> list = customerAddrMapper.getByCustomerId(customerId);
        return list;
    }

    /**
     * 根据地址id查询地址信息
     * @param addressId
     * @return
     */
    public CustomerAddr getAddress(String addressId) {
        //log.info("根据地址id查询地址信息：{}",addressId);
        return customerAddrMapper.getByAddressId(addressId);
    }

    /**
     * 修改地址
     * @param addressDTO
     */
    @Transactional
    public Boolean updateAddress(AddressDTO addressDTO) {
        CustomerAddr customerAddr = new CustomerAddr();
        BeanUtils.copyProperties(addressDTO,customerAddr);
        customerAddr.setCustomerId(String.valueOf(BaseContext.getCurrentId()));
        //唯一的默认地址不能修改为非默认地址
        if(getDefaultByCustomerId().getAddressId().equals(addressDTO.getAddressId()) && addressDTO.isDefault()==false){
            return false;
            //throw new AddressBookBusinessException(MessageConstant.UPDATE_ADDRESS_FAIL);
        }
        log.info("修改地址：{}",customerAddr);
        if(addressDTO.isDefault()==true) {
            setDefault(customerAddr);
        }
        customerAddrMapper.update(customerAddr);
        return true;
    }

    /**
     * 设置默认地址
     *
     * @param customerAddr
     */
    @Transactional
    public void setDefault(CustomerAddr customerAddr) {
        //1、将当前用户的所有地址修改为非默认地址
        customerAddr.setDefault(false);
        customerAddrMapper.updateIsDefaultByCustomerId(customerAddr);

        //2、将当前地址改为默认地址
        customerAddr.setDefault(true);
        customerAddrMapper.setDefaultByAddressId(customerAddr);
    }

    @Override
    public Boolean deleteAddress(String addressId) {
        //不能删除唯一的默认地址
        if(getDefaultByCustomerId().getAddressId().equals(addressId)){
            return false;
        }
        customerAddrMapper.deleteByAddressId(addressId);
        return true;
    }

    public CustomerAddr getDefaultByCustomerId(){
        String customerId = BaseContext.getCurrentId().toString();
        CustomerAddr customerAddr = customerAddrMapper.getDefaultByCustomerId(customerId);
        return customerAddr;
    }

    @Override
    public List<CustomerAddr> listpickup() {
        return customerAddrMapper.listpickup();
    }
}
